/**
 *
 * \file DEBUG.c
 * @author        Andreas Schlueter
 */

#include "../../../../System/src/ScatterWeb.System.h"
#include "debug.h"



/////////////////////////Prototypes




////////////////////////Globals
char buffer[DEB_LINE_SIZE];
UINT8 pos;

/**
 * sends data to monitoring node
 */
void _debug_send(UINT8* data, UINT8 size){
  //end of string:
  buffer[size] = 0;

  //define a packet
  packet_t p;
  // fill the packet
  p.to = DEB_NODE;
  p.from = Configuration.id;
  p.type = DEBUG_MESSAGE;
  p.header = 0;
  p.header_length = 0;
  p.data = data;
  p.data_length = size+1;
  // sends a packet
  Net_send(&p, NULL);
}

void writer(UINT8 data) {
  buffer[pos++] = data;
  if(pos == DEB_LINE_SIZE-1){
    _debug_send((UINT8*)buffer, DEB_LINE_SIZE-1);
    pos = 0;
  }
}


/**
 * parses and sends textto monitoring node
 */
void debug_send(const char* fmt, ...){

  pos = 0;
  va_list argp;
  va_start(argp, fmt);
  String_abstractWrite(writer, fmt, argp);
  va_end(argp);

  //flush remaining data
  _debug_send((UINT8*)buffer, pos);
}
